package com.itheima.web.service.impl.oms;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.oms.OrderReturnApplyDto;
import com.itheima.service.oms.IOmsOrderReturnApplyService;
import com.itheima.utils.PageResult;
import com.itheima.vo.oms.OmsCompanyAddress;
import com.itheima.vo.oms.OmsOrderReturnApply;
import com.itheima.vo.oms.OrderReturnApplyVo;
import com.itheima.web.mapper.oms.OmsCompanyAddressMapper;
import com.itheima.web.mapper.oms.OmsOrderReturnApplyMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 订单退货申请 服务实现类
 * </p>
 *
 * @author 王志斌
 * @since 2022-07-06
 */
@DubboService
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply> implements IOmsOrderReturnApplyService {
    @Autowired
    private OmsOrderReturnApplyMapper orderReturnApplyMapper;
    @Autowired
    private OmsCompanyAddressMapper companyAddressMapper;

    //根据退货申请id，查询退货申请详情
    @Override
    public OrderReturnApplyVo getOrderReturn(Integer id) {
        OmsOrderReturnApply returnApply = orderReturnApplyMapper.selectById(id.longValue());
        Long companyAddressId = returnApply.getCompanyAddressId();
        OmsCompanyAddress companyAddress = companyAddressMapper.selectById(companyAddressId);
        return OrderReturnApplyVo.init(returnApply, companyAddress);
    }

    //批量删除退货申请
    @Override
    public Integer delete(List<Long> ids) {
        return orderReturnApplyMapper.deleteBatchIds(ids);
    }

    //修改退货申请状态
    @Override
    public Integer updateStatus(Integer id, OmsOrderReturnApply orderReturnApply) {
        orderReturnApply.setId(id.longValue());
        orderReturnApply.setHandleTime(LocalDateTime.now());
        if (orderReturnApply.getStatus() == 2) {
            orderReturnApply.setReceiveTime(LocalDateTime.now());
        }
        return orderReturnApplyMapper.updateById(orderReturnApply);
    }

    @Override
    public PageResult pagelist(OrderReturnApplyDto dto) {
        Page<OmsOrderReturnApply> applyPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<OmsOrderReturnApply> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dto.getId() != null, OmsOrderReturnApply::getId, dto.getId());
        lambdaQueryWrapper.eq(dto.getStatus() != null, OmsOrderReturnApply::getStatus, dto.getStatus());
        lambdaQueryWrapper.like(dto.getHandleMan() != null, OmsOrderReturnApply::getHandleMan, dto.getHandleMan());
        lambdaQueryWrapper.like(dto.getCreateTime() != null, OmsOrderReturnApply::getCreateTime, dto.getCreateTime());
        lambdaQueryWrapper.like(dto.getHandleTime() != null, OmsOrderReturnApply::getHandleTime, dto.getHandleTime());
        orderReturnApplyMapper.selectPage(applyPage, lambdaQueryWrapper);
        return PageResult.init(applyPage.getTotal(), dto.getPageNum().longValue(), dto.getPageSize().longValue(), applyPage.getRecords());
    }
}
